﻿using WinRTFramework.Interfaces.Contracts.Sharing;
using Windows.ApplicationModel.DataTransfer;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace WinRTFramework.FrameworkCore.Managers
{
    internal class SharingSourceManager
    {
        public SharingSourceManager()
        {
            DataTransferManager.GetForCurrentView().DataRequested += SharingSourceManager_DataRequested;
        }

        void SharingSourceManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            var currentPage = ((Frame)Window.Current.Content).Content as Page;
            if(currentPage == null)
            {
                args.Request.FailWithDisplayText("No page available");
                return;
            }

            ISupportSharingSourceContract sharingSource = currentPage.DataContext as ISupportSharingSourceContract;
            if(sharingSource == null)
            {
                args.Request.FailWithDisplayText("Current page does not support sharing");
                return;
            }

            sharingSource.OnShareRequested(args.Request);
        }
    }
}
